﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.SearchControls
{
    public partial class SearchPhieuNhapKho : UserControl
    {
        private DataGridView _dgvPhieuNhapKho;
        private bool _showAll = true;

        public DataGridView DgvPhieuNhapKho
        {
            get { return _dgvPhieuNhapKho; }
            set { _dgvPhieuNhapKho = value; }
        }

        public SearchPhieuNhapKho()
        {
            InitializeComponent();

            // event check change
            foreach (Control c in this.Controls)
                if (c.GetType() == typeof(CheckBox))
                {
                    ((CheckBox)c).CheckedChanged += this.CheckedChanged;
                }

            // load hanghoa
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            foreach (_DB.HangHoa h in dc.HangHoas)
            {
                clbHangHoa.Items.Add(h);
            }

            // load ncc
            foreach (_DB.NhaCungCap ncc in dc.NhaCungCaps)
            {
                cbbNhaCungCap.Items.Add(ncc.TenNhaCungCap);
            }

            // load nguoi lap, nguoi duyet
            foreach (_DB.NguoiDung u in dc.NguoiDungs)
            {
                cbbNguoiTao.Items.Add(u.Ten);
            }

            // load phieu mua hang
            var listPhieuMH = from qt in dc.QuyTrinhMuaHangs
                              where qt.deleted == false &&
                                    qt.PhieuMuaHang != null
                              orderby qt.PhieuMuaHang.NgayTao descending
                              select qt.PhieuMuaHang;
            foreach (_DB.PhieuMuaHang p in listPhieuMH)
            {
                cbbPhieuMuaHang.Items.Add(p.MaPhieu);
            }

            reloadChecked();
        }

        private void CheckedChanged(object sender, EventArgs e)
        {
            reloadChecked();
        }

        private void reloadChecked()
        {
            dtpNgayTaoMin.Enabled = cbNgayTaoMin.Checked;
            dtpNgayTaoMax.Enabled = cbNgayTaoMax.Checked;
            cbbNguoiTao.Enabled = cbNguoiTao.Checked;
            cbbPhieuMuaHang.Enabled = cbPhieuMuaHang.Checked;
            clbHangHoa.Enabled = cbHangHoa.Checked;
            cbbNhaCungCap.Enabled = cbNhaCungCap.Checked;
        }

        IQueryable<_DB.PhieuNhapKho> getListPhieu()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            IQueryable<_DB.PhieuNhapKho> listPhieuNK = 
                              from p in dc.PhieuNhapKhos
                              where p.DonDatHang.QuyTrinhMuaHang.deleted == false &&
                                    p.deleted == false
                              orderby p.NgayTao descending
                              select p;


            if (!Program.NguoiDungHienTai.coQuyen(39))
                listPhieuNK = listPhieuNK.Where(x => x.MaNguoiTao == Program.MaNguoiDungHienTai);

            return listPhieuNK;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            _showAll = false;
            var listPhieuNK = getListPhieu();

            // filter
            var listPhieu = listPhieuNK.Where(x => !dtpNgayTaoMin.Enabled || x.NgayTao.Date >= dtpNgayTaoMin.Value.Date);
            listPhieu = listPhieu.Where(x => !dtpNgayTaoMax.Enabled || x.NgayTao.Date <= dtpNgayTaoMax.Value.Date);

            listPhieu = listPhieu.Where(x => !cbbNguoiTao.Enabled || x.NguoiTao.Ten.Contains(cbbNguoiTao.Text));
            listPhieu = listPhieu.Where(x => !cbbNhaCungCap.Enabled || x.DonDatHang.NhaCungCap.TenNhaCungCap.Contains(cbbNhaCungCap.Text));
            listPhieu = listPhieu.Where(x => !cbbPhieuMuaHang.Enabled || x.DonDatHang.QuyTrinhMuaHang.MaPhieuMuaHang.Contains(cbbPhieuMuaHang.Text));

            if (clbHangHoa.Enabled)
            foreach (Object o in clbHangHoa.CheckedItems)
            {
                String maNHangHoa = ((_DB.HangHoa)o).MaHangHoa;
                listPhieu = listPhieu.Where(x => x.DonDatHang.HangHoaDonDatHangs.Count(y => y.PhieuDeNghiMuaHang_HangHoa.MaHangHoa == maNHangHoa) > 0);
            }

            loadDataGrid(listPhieu);
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            _showAll = true;
            loadDataGrid(getListPhieu());
        }

        private void loadDataGrid(IQueryable<_DB.PhieuNhapKho> listPhieu)
        {
            int count = 1;
            _dgvPhieuNhapKho.Rows.Clear();
            foreach (_DB.PhieuNhapKho p in listPhieu)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(_dgvPhieuNhapKho);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = p.MaPhieu;
                r.Cells[2].Value = p.NguoiTao.Ten;
                r.Cells[3].Value = p.NgayTao;
                r.Cells[4].Value = p.DonDatHang.HangHoaDonDatHangs.Count;

                _dgvPhieuNhapKho.Rows.Add(r);
            }
        }

        public void refreshGrid()
        {
            if (_showAll)
                btnAll_Click(null, null);
            else
                btnSearch_Click(null, null);
        }

        private void SearchPhieuNhapKho_Load(object sender, EventArgs e)
        {

        }
    }
}
